package com.helen.ui.newui;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.helen.db.UserHelper;
import com.helen.entity.MyRecoveryEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.RxPayUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2019/3/26
 */
public class BuyRecoveryCarActivity extends BaseActivity {
    @BindView(R.id.tv_paid_money)
    TextView tvPaidMoney;
    @BindView(R.id.cb_zhifubao)
    CheckBox cbZhifubao;
    @BindView(R.id.cb_weixin)
    CheckBox cbWeixin;
    @BindView(R.id.cb_yue)
    CheckBox cbYue;
    @BindView(R.id.tv_sure_paid)
    SuperTextView tvSurePaid;
    private MyRecoveryEntity myRecoveryEntity;
    private String type = "1";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_buy_car;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        myRecoveryEntity = (MyRecoveryEntity) getIntent().getSerializableExtra("myRecoveryEntity");
        getToolbarTitle().setText("购买");
        tvPaidMoney.setText(myRecoveryEntity.getMoney() + "");
    }

    @Override
    protected void updateViews() {

    }

    private void buyCar() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("userId", UserHelper.getInstance().getloginEntity(BuyRecoveryCarActivity.this).getId());
        params.put("carId", myRecoveryEntity.getId() + "");
        params.put("type", type);
        params.put("money", myRecoveryEntity.getMoney() + "");
        EasyHttp.post(Constants.API_JSONAPI_PAY_CAR)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "支付失败==" + e.toString());
                        MToast.makeTextShort(BuyRecoveryCarActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "支付==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            String mResult = JsonUtil.getJsonValuesString(result, "result");
                            if (type.equals("1")) {
                                RxPayUtils.aLiPay(BuyRecoveryCarActivity.this, mResult);
                            } else if (type.equals("2")) {
                                RxPayUtils.wechatPay(BuyRecoveryCarActivity.this, mResult);
                            } else {
                                MyLog.d("余额支付");
                                MToast.makeTextLong(BuyRecoveryCarActivity.this, "购买成功").show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            }

                        } else {
                            MToast.makeTextShort(BuyRecoveryCarActivity.this, msg).show();
                        }

                    }
                });
    }


    @OnClick({R.id.cb_zhifubao, R.id.cb_weixin, R.id.cb_yue, R.id.tv_sure_paid})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cb_zhifubao:
                cbZhifubao.setChecked(true);
                cbWeixin.setChecked(false);
                cbYue.setChecked(false);
                type = "1";
                break;
            case R.id.cb_weixin:
                type = "2";
                cbWeixin.setChecked(true);
                cbZhifubao.setChecked(false);
                cbYue.setChecked(false);
                break;
            case R.id.cb_yue:
                type = "3";
                cbWeixin.setChecked(false);
                cbZhifubao.setChecked(false);
                cbYue.setChecked(true);
                break;
            case R.id.tv_sure_paid:
                buyCar();
                break;
        }
    }
}
